package com.fmsz.xmly.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Scroller;

import java.util.Map;

/**
 * Created by Administrator on 2016/12/7.
 */

public class SlidingFinshLayout extends LinearLayout implements View.OnTouchListener {

    private Scroller scroller;
    private int touchSlop;
    private ViewGroup parent;
    private int tempX;
    private int downX,downY,viewWidth;
    private boolean isSliding,isFinsh;
    private View touchView;
    private slidingFinshListener listener;

    public SlidingFinshLayout(Context context) {
        this(context,null);
    }

    public SlidingFinshLayout(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SlidingFinshLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        scroller = new Scroller(context);
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            parent = ((ViewGroup) this.getParent());
            viewWidth = this.getWidth();
        }
    }

    public slidingFinshListener getListener() {
        return listener;
    }

    public void setListener(slidingFinshListener listener) {
        this.listener = listener;
    }

    public View getTouchView() {
        return touchView;
    }

    public void setTouchView(View touchView) {
        this.touchView = touchView;
        touchView.setOnTouchListener(this);
    }

    //滚出界面
    private void scrollRight(){
        final int delta = (viewWidth+parent.getScrollX());
        scroller.startScroll(parent.getScrollX(),0,-delta+1,0,Math.abs(delta));
        postInvalidate();
    }
    //回到起始位置
    private void scrollorigin(){
        int delta = parent.getScrollX();
        scroller.startScroll(parent.getScrollX(),0,-delta,0,Math.abs(delta));
        postInvalidate();
    }
    private boolean isTouchAbsListView(){
        return touchView instanceof AbsListView?true:false;
    }
    private boolean isTouchScroll(){
        return touchView instanceof ScrollView?true:false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = tempX = ((int) event.getRawX());
                downY = ((int) event.getRawY());
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = ((int) event.getRawX());
                int deltaX = tempX-moveX;
                tempX = moveX;
                if (Math.abs(moveX-downY)>touchSlop && Math.abs((int) event.getRawY() - downY) < touchSlop) {
                    isSliding = true;
                    if (isTouchAbsListView()) {
                        MotionEvent cancelEvent = MotionEvent.obtain(event);
                        cancelEvent
                                .setAction(MotionEvent.ACTION_CANCEL
                                        | (event.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                        v.onTouchEvent(cancelEvent);
                    }
                }
                if (moveX - downX >= 0 && isSliding) {
                    parent.scrollBy(deltaX, 0);

                    // 屏蔽在滑动过程中ListView ScrollView等自己的滑动事件
                    if (isTouchScroll() || isTouchAbsListView()) {
                        return true;
                    }
                }

                break;
            case MotionEvent.ACTION_UP:
                isSliding = false;
                if (parent.getScrollX() <= -viewWidth / 2) {
                    isFinsh = true;
                    scrollRight();
                } else {
                    scrollorigin();
                    isFinsh = false;
                }
                break;
        }
        if (isTouchScroll() || isTouchAbsListView()) {
            return v.onTouchEvent(event);
        }
        return true;
    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            parent.scrollTo(scroller.getCurrX(), scroller.getCurrY());
            postInvalidate();
            if (scroller.isFinished()) {

                if (listener != null && isFinsh) {
                    listener.onslidingFinsh();
                }
            }
        }
    }

    public interface slidingFinshListener{
      public void onslidingFinsh();
    }
}
